package cj.web.sc.util;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import cj.utils.Constants;
import cj.utils.weixin.HttpsWenXinUtil;
import cj.web.cc.controllers.BaseController;

public class SendWXTemplate extends BaseController{

	
	/**
	 * 发送模板消息
	 * https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
	 *  {
           "touser":"OPENID",
           "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
           "url":"http://weixin.qq.com/download",            
           "data":{
                   "first": {
                       "value":"恭喜你购买成功！",
                       "color":"#173177"
                   },
                   "keynote1":{
                       "value":"巧克力",
                       "color":"#173177"
                   },
                   "keynote2": {
                       "value":"39.8元",
                       "color":"#173177"
                   },
                   "keynote3": {
                       "value":"2014年9月22日",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"欢迎再次购买！",
                       "color":"#173177"
                   }
           }
       }
     *
     *	模版ID3eafgX5GTKCKJatSfhb9--Cnoms-o_cvcJtOr4BLLsQ
		开发者调用模版消息接口时需提供模版ID
		标题订单支付成功
		行业IT科技 - 互联网|电子商务
		详细内容
		{{first.DATA}}
		支付金额：{{orderMoneySum.DATA}}
		商品信息：{{orderProductName.DATA}}
		{{Remark.DATA}}
		在发送时，需要将内容中的参数（{{.DATA}}内为参数）赋值替换为需要的信息
		内容示例
		我们已收到您的货款，开始为您打包商品，请耐心等待: )
		支付金额：30.00元
		商品信息：我是商品名字
		
		如有问题请致电400-828-1878或直接在微信留言，小易将第一时间为您服务！
	 * @param touser
	 * @return
	 * @throws IOException 
	 * @throws NoSuchAlgorithmException 
	 * @throws KeyManagementException 
	 */
	public static JSONObject sendWx(String touser,String template_id,String url,
			String first,String orderMoneySum,String orderProductName,String remark) 
			throws KeyManagementException, NoSuchAlgorithmException, IOException{
		
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
		
		JSONObject jo = new JSONObject();
		jo.put("touser", touser);
		jo.put("template_id", template_id);
		jo.put("url", url);
		
		Map dataReps = new HashMap();
		//详细内容
		Map firstReps = new HashMap();
		firstReps.put("value", first);
		firstReps.put("color", "#173177");
		//支付金额
		Map orderMoneySumReps = new HashMap();
		orderMoneySumReps.put("value", orderMoneySum);
		orderMoneySumReps.put("color", "#173177");
		//商品信息
		Map orderProductNameReps = new HashMap();
		orderProductNameReps.put("value", orderProductName);
		orderProductNameReps.put("color", "#173177");
		//备注
		Map remarkReps = new HashMap();
		remarkReps.put("value", remark);
		remarkReps.put("color", "#173177");
		
		dataReps.put("first",firstReps);
		dataReps.put("orderMoneySum",orderMoneySumReps);
		dataReps.put("orderProductName",orderProductNameReps);
		dataReps.put("Remark",remarkReps);
		
		jo.put("data", dataReps);
		String requestContent = jo.toString();
		String access_token = HttpsWenXinUtil.initWeiXin(APPID,APPSECRET);
		
		//请求远端微信Http接口
		byte[] xml = HttpsWenXinUtil.post(requestUrl+=access_token, requestContent, Constants.charset);
		return new JSONObject().fromObject(new String(xml, Constants.charset));
	}
	
	public static void main(String[] arges){
		String touser ="opu3Gs1OxzPCX6qnW2tHsg9m_tk0";
		String template_id ="3eafgX5GTKCKJatSfhb9--Cnoms-o_cvcJtOr4BLLsQ";
		String url ="";
		//详细内容
		String first ="楚才报名!";
		//支付金额
		String orderMoneySum ="30.00元";
		//商品信息
		String orderProductName ="楚才竞赛报名!";
		//备注
		String remark ="楚才竞赛";
		try {
			sendWx(touser, template_id, url, first, orderMoneySum, orderProductName, remark);
		} catch (KeyManagementException | NoSuchAlgorithmException
				| IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}